﻿'''
Created on May 4, 2011

@author: Jialai_Zhu
'''
#!/usr/bin/python 
# -*- coding: gb2312 -*- 
# 
import re
class match_result():
    def __init__(self,match_string,extract_part):
        self.match_string=match_string
        self.extract_part=extract_part
class reg_ptn():
    def __init__(self,ptn='',index=0,name='',handle=None):
        self.name=name
        self.handle=handle
        self.ptn=ptn
        self.index=index
        self.re=re.compile(self.ptn)
    def search_all(self,string,handle=None):
        ret=[]
        for match in self.re.finditer(string):
            try:
                s=match.start()
                e=match.end()
                g=match.group()     
                if handle!=None:
                    ret.append(handle(match))
                else:               
                    ret.append(match_result(match.group(),match.group(self.index)))
            except Exception,ex:
                pass
        return ret
    def search(self,string):      
        try:
            match = self.re.search(string)
            g=match.group()                    
            return match_result(match.group(),match.group(self.index))
        except Exception,ex:
            pass
        return None              
class reg_ptn_template():
    def __init__(self,template,index,arg_list_dic={},name='',handle=None):
        self.ptn=[]
        for v in arg_list_dic:
            for value in arg_list_dic[v]:
                ptn=template%({v:value})
                self.ptn.append(reg_ptn(index=index,ptn=ptn,name=name,handle=handle))
        if len(self.ptn)==0:
            self.ptn.append(reg_ptn(index=index,ptn=template,name=name,handle=handle))
        pass

class reg_match():
    GROUP_INDEX=0
    def __init__(self,list=[],ptn=None):
        self.reg_ptn_templates=[]
        self.reg_ptn=[]
        self.add_pattern(ptn)
        self.add_pattern_list(list)
    def add_pattern_list(self,pattern_list):
        for object in pattern_list:
            self.reg_ptn_templates.append(object)
    def add_pattern(self,reg_ptn=None):
        if reg_ptn==None:return
        self.reg_ptn.append(reg_ptn)
    def search_all(self,string):
        for template in self.reg_ptn_templates:
            for ptn in template.ptn:
                ret=ptn.search_all(string)
                if len(ret):
                    return ret
        for ptn in self.reg_ptn:
            ret=ptn.search_all(string)
            if len(ret):
                return ret                
        return []
    def search(self,string):
        for template in self.reg_ptn_templates:
            for ptn in template.ptn:
                ret=ptn.search(string)
                if ret!=None:
                    return ret
        for ptn in self.reg_ptn:
            ret=ptn.search(string)
            if ret!=None:
                return ret                 
        return None

sampel=[       u'<TITLE>中国国际股票投资总公司',
               u'<title>中国国际股票投资总公司',
               u'<title>中国国际股票投资总公司股票投资公司dfd',
               u'龙溪股份:汽车下乡利好飙升-北京展华证券股份有限公司- 中国股票证券 ...',
               u'齐鲁证券：维持鲁西化工“买入”评级_黑马推荐_上海安信证券投资有限公司']


            
def get_unicode(string):
    if isinstance(string,unicode):
        return string
    for encode in ['gb2312','gbk','utf8']:
        try:
            return unicode(string,encode)
        except:
            pass
    print 'none'
    return None          
if __name__ == '__main__':
    pass
    